package top.fsb.im.sys;

import top.fsb.im.common.SpringContextHolder;
import top.fsb.im.sys.entity.ImUser;
import top.fsb.im.sys.service.IImUserService;
import top.fsb.im.sys.service.impl.ImUserServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用户 Utils 类
 * @author 乐天
 * @since 2019-01-30
 */
public class UserUtils {

    private static IImUserService userService = SpringContextHolder.getBean(ImUserServiceImpl.class);

    /**
     * 从系统中读取用户
     *
     * @return 用户信息
     */
    public static ImUser getUser() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        return userService.getByLoginName(userDetails.getUsername());
    }

    /**
     * 获取用户id
     * @param id id
     * @return 结果
     */
    public static ImUser getUser(String id) {
        return userService.getById(id);
    }
}
